//
//  ActivityDetailTableView.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/26.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "ActivityDetailTableView.h"
#import "ActivityModel.h"
@implementation ActivityDetailTableView {
    
    ActivityModel *activityModel;
    
    UIButton *notStartButton;
    UIButton *orderButton;
    UILabel *numberLabel;
    
}


- (void) initView:(ActivityModel *)activityModel {
    
    self.rowHeight = UITableViewAutomaticDimension;
    
    self->activityModel = activityModel;
    
    [self initHeaderView];
    
    [self reloadData];
    
}


- (void) initHeaderView {
    UIView *headerView = UIView.new;
    
    
     SDCycleScrollView *cycleScrollView = [ SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_WIDTH) delegate:nil placeholderImage:nil];
    //cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    // 设置pageControl居右，默认居中
    cycleScrollView.autoScrollTimeInterval = 3;// 自定义轮播时间间隔
    
    NSMutableArray * sliderImgUrl = NSMutableArray.new;
    for (ImageModel* imageModel in self->activityModel.goods.images) {
        [sliderImgUrl addObject: imageModel.img_url];
    }
    
    cycleScrollView.imageURLStringsGroup = sliderImgUrl;
    
    [headerView addSubview:cycleScrollView];
    
    UILabel *activityNameLabel = UILabel.new;
    activityNameLabel.text = self->activityModel.title;
    activityNameLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    activityNameLabel.textColor = MAIN_FONT_COLOR;
    activityNameLabel.numberOfLines = 0;
    activityNameLabel.preferredMaxLayoutWidth = MAIN_SCREEN_WIDTH - 20;
    [headerView addSubview:activityNameLabel];

    [activityNameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cycleScrollView.bottom).offset(10);
        make.left.equalTo(headerView.left).offset(10);
        make.right.equalTo(headerView.right).offset(-10);

    }];


    UIImageView *priceImage = UIImageView.new;
    priceImage.image = [UIImage imageNamed:@"money_0"];
    [headerView addSubview:priceImage];

    [priceImage makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(activityNameLabel.bottom).offset(10);
        make.centerX.equalTo(headerView.centerX);
        make.width.equalTo(32);
        make.height.equalTo(32);
    }];

    UILabel *marketPriceLabel = UILabel.new;
    marketPriceLabel.textAlignment = NSTextAlignmentCenter;
    marketPriceLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
    marketPriceLabel.textColor = UIColorFromRGB(0x999999);
    marketPriceLabel.numberOfLines = 1;
    // 中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"市场价：%d", self->activityModel.goods.price] attributes:attribtDic];
    marketPriceLabel.attributedText = attribtStr;
    [headerView addSubview:marketPriceLabel];

    [marketPriceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(priceImage.centerY);
        make.left.equalTo(priceImage.right).offset(10);
    }];

    UILabel *numberDescLabel = UILabel.new;
    numberDescLabel.textAlignment = NSTextAlignmentCenter;
    numberDescLabel.text = [NSString stringWithFormat:@"%d/%@", self->activityModel.number, self->activityModel.goods.unit];
    numberDescLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:14];
    numberDescLabel.textColor = UIColorFromRGB(0x999999);
    numberDescLabel.numberOfLines = 1;
    [headerView addSubview:numberDescLabel];

    [numberDescLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceImage.bottom).offset(10);
        make.centerX.equalTo(priceImage.centerX);
    }];

    self->notStartButton = UIButton.new;
    self->notStartButton.hidden = YES;
    self->notStartButton.userInteractionEnabled = NO;
    self->notStartButton.layer.cornerRadius = 5;
    self->notStartButton.layer.borderColor = [UIColorFromRGB(0x36B85D) CGColor];
    self->notStartButton.layer.borderWidth = 1/[[UIScreen mainScreen] scale];
    self->notStartButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self->notStartButton setTitle:@"距离开抢  00 : 00 : 00" forState:UIControlStateNormal];
    [self->notStartButton setTitleColor:UIColorFromRGB(0x36B85D) forState:UIControlStateNormal];
    self->notStartButton.titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    [headerView addSubview:self->notStartButton];

    [self->notStartButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberDescLabel.bottom).offset(8);
        make.centerX.equalTo(headerView.centerX);
        make.width.equalTo(220);
        make.height.equalTo(40);
    }];


    self->orderButton = UIButton.new;
    [self->orderButton addTarget:self action:@selector(clickOrder) forControlEvents:UIControlEventTouchUpInside];
    self->orderButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self->orderButton setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    self->orderButton.titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    self->orderButton.layer.cornerRadius = 5;
    [self->orderButton setTitle:@"立刻抢购" forState:UIControlStateNormal];
    self->orderButton.backgroundColor = MAIN_COLOR;
    [headerView addSubview:self->orderButton];

    // 参与抢购人数
    self->numberLabel = [UILabel new];
    self->numberLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
    self->numberLabel.textColor = UIColorFromRGB(0x999999);
    self->numberLabel.numberOfLines = 1;
    //self->numberLabel.hidden = YES;
    [headerView addSubview:self->numberLabel];

    [self->numberLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->orderButton.centerY);
        make.left.equalTo(self->orderButton.right).offset(6);
    }];

    if (self->activityModel.status == 1) {
        self->notStartButton.hidden = NO;
        self->orderButton.enabled = NO;
        self->orderButton.hidden = YES;
    } else if (self->activityModel.status == 2) {

//        if ([Global getInstance].user.points < self->activityModel.points) {
//            self->orderButton.enabled = NO;
//            [self->orderButton setTitle:@"积分不足" forState:UIControlStateNormal];
//        } else {
//            [self->orderButton setTitle:@"立刻抢购" forState:UIControlStateNormal];
//        }
//
//        self->orderButton.backgroundColor = MAIN_COLOR;
//    } else if (self->activityModel.status == 3) {
//        self->orderButton.enabled = NO;
//        self->numberLabel.hidden = NO;
//        self->numberLabel.text = [Tool getNumberString:self->activityModel.number];
//        [self->orderButton setTitle:@"已结束" forState:UIControlStateNormal];
//        self->orderButton.backgroundColor = UIColorFromRGB(0x999999);
//
    }

    if (self->activityModel.user_status == 2) {
        self->orderButton.enabled = NO;
        [self->orderButton setTitle:@"抢购成功" forState:UIControlStateNormal];
        self->orderButton.backgroundColor = MAIN_COLOR;
    }

    if (self->activityModel.user_status == 3) {
        self->orderButton.enabled = NO;
        [self->orderButton setTitle:@"已抢完" forState:UIControlStateNormal];
        self->orderButton.backgroundColor = UIColorFromRGB(0x999999);

    }


    int offset = -10;
    if (self->activityModel.points > 0) {
        offset = -30;
    }

    [self->orderButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberDescLabel.bottom).offset(8);
        make.centerX.equalTo(headerView.centerX);
        make.width.equalTo(180);
        make.height.equalTo(40);
        make.bottom.equalTo(headerView).offset(offset);
    }];

    // 积分提示
    UIImageView *pointsViewIcon = UIImageView.new;
    pointsViewIcon.image = [UIImage imageNamed:@"point"];
    [headerView addSubview:pointsViewIcon];

    [pointsViewIcon makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->orderButton.bottom).offset(10);
        make.left.equalTo(self->orderButton.left).offset(0);
        make.width.equalTo(12);
        make.height.equalTo(11);
    }];


    UILabel *pointsViewTips = UILabel.new;
    pointsViewTips.textAlignment = NSTextAlignmentCenter;
    pointsViewTips.text = [NSString stringWithFormat:@"历史积分需要%d才能抢购哦！", self->activityModel.points];
    pointsViewTips.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
    pointsViewTips.textColor = UIColorFromRGB(0xD8B200);
    pointsViewTips.numberOfLines = 1;
    [headerView addSubview:pointsViewTips];

    [pointsViewTips makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pointsViewIcon.centerY);
        make.left.equalTo(pointsViewIcon.right).offset(3);
    }];
    
    if (self->activityModel.points == 0) {
        pointsViewTips.hidden = YES;
    }


    CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headerView.frame;
    frame.size.height = height;
    headerView.frame = frame;
    
   // NSLog(@"%f", height);
    
    self.tableHeaderView = headerView;
}


- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    
    if (self) {
        
            self.separatorStyle = UITableViewCellSeparatorStyleNone;
            self.dataSource = self;
            self.delegate = self;
            
          //  [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//        return 500;
//}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier;
        
        CellIdentifier = @"CommentTableViewCell";
        
        CommentTableViewCell *cell = (CommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                               reuseIdentifier:CellIdentifier];
        }
    
 CommentModel *commentModel = [activityModel.default_comments objectAtIndex:indexPath.row];
    [cell setComment:commentModel];
        
        return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return  [activityModel.default_comments count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 46;
}


//表头的设置
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        
        SectionGoodsCommentView *sectionView = [[SectionGoodsCommentView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 36)];
        [sectionView initView:self->activityModel.default_comments.count];
        
        return sectionView;
    
}


- (void) clickOrder{
    
    OrderConfirmViewController *orderConfirmViewController = [[OrderConfirmViewController alloc]init];
    
    orderConfirmViewController.imgUrl = self->activityModel.goods.img_url;
    orderConfirmViewController.activityTitle = self->activityModel.title;
    orderConfirmViewController.price = self->activityModel.goods.price;
    //orderConfirmViewController.id = order.id;
    
     orderConfirmViewController.hidesBottomBarWhenPushed = YES;
   [[[AppDelegate shareAppDelegate] shareNavigationController] pushViewController:orderConfirmViewController animated:YES];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



@end
